package com.tywj.lib.social.share

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.blankj.utilcode.util.AppUtils
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXImageObject
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import com.tywj.lib.core.common.util.BitmapUtil
import com.tywj.lib.core.common.util.ToastUtil
import com.tywj.lib.social.R
import com.tywj.lib.social.SocialUtil
import com.tywj.lib.social.entity.ImageShareEntity
import com.tywj.lib.social.entity.WebPageShareEntity
import com.tywj.lib.social.interfaces.Callback
import com.tywj.lib.social.interfaces.IShare
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/16
 *     desc   : 微信分享
 *     微信开发平台： https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Share_and_Favorites/Android.html
 *     version: 1.0.0
 * </pre>
 */
class WeChatShare : IShare {

    override fun shareWebPage(entity: WebPageShareEntity, callback: Callback?) {
        if (isWxAppNotInstalledOrSupport()) return

        SocialUtil.share.callback = callback

        val webPage = WXWebpageObject()
        webPage.webpageUrl = entity.url

        val msg = WXMediaMessage()
        msg.title = entity.title
        msg.description = entity.content

        doAsync {
            val bitmap = BitmapUtil.getBitmapFromUrl(entity.imgUrl, 100, 100)
            if (bitmap != null) {
                msg.thumbData = BitmapUtil.bitmap2ByteArray(bitmap)
            }
            uiThread {
                val req = SendMessageToWX.Req()
                // 标识唯一请求
                req.transaction = buildTransaction("webPage")
                msg.mediaObject = webPage
                req.message = msg
                req.scene = SendMessageToWX.Req.WXSceneSession
                SocialUtil.wxApi.sendReq(req)
            }
        }
    }

    //检查微信支持状态
    private fun isWxAppNotInstalledOrSupport(): Boolean {
        // 判断用户是否安装微信客户端
        if (!SocialUtil.wxApi.isWXAppInstalled) {
            ToastUtil.shortToast(R.string.social_not_install_wechat)
            return true
        }

        // 判断用户安装的微信是否支持分享
        if (SocialUtil.wxApi.wxAppSupportAPI <= AppUtils.getAppVersionCode()) {
            ToastUtil.shortToast(R.string.social_not_support_wechat_share)
            return true
        }
        return false
    }

    private fun buildTransaction(type: String?): String {
        return if (type == null) System.currentTimeMillis().toString() else type + System.currentTimeMillis()
    }

    override fun shareImage(entity: ImageShareEntity, callback: Callback?) {
        if (isWxAppNotInstalledOrSupport()) return

        SocialUtil.share.callback = callback

        val imageObject = WXImageObject()
        imageObject.setImagePath(entity.imageUrl)
        val msg = WXMediaMessage()
        msg.mediaObject = imageObject

        doAsync {
            val bmp = BitmapFactory.decodeFile(entity.imageUrl)
            //设置缩略图
            val thumbBmp = Bitmap.createScaledBitmap(bmp, 100, 100, true)
            msg.setThumbImage(thumbBmp)
            bmp.recycle()

            uiThread {
                val req = SendMessageToWX.Req()
                req.transaction = buildTransaction("img")
                req.message = msg
                req.scene =
                    if (entity.sendtype == 0) {
                        SendMessageToWX.Req.WXSceneSession
                    } else SendMessageToWX.Req.WXSceneTimeline
                SocialUtil.wxApi.sendReq(req)
            }
        }
    }
}